package com.jump.userweb.controller;

import com.jump.userweb.service.UserService;
import com.jump.userweb.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by fangxiao on 15/9/8.
 */


@Controller
@RequestMapping("/user/page")
public class UserPageController {
    @Autowired
    UserService userService ;





    /**
     *
     * @return
     */
    @RequestMapping(value="/register", method= RequestMethod.POST)
    public String register(@RequestParam String username,
                             @RequestParam String mobile,
                             @RequestParam String password,
                             @RequestParam String email,
                             HttpSession session,
                             HttpServletResponse response,
                             Model model) {
        Map map = userService.getRegisterMap(username, password, mobile, email);
        Util.handleWebUser(session,response, map);

        model.addAttribute("registerMap",map);
        return "result";
    }

    @RequestMapping("/toregister")
    public String toRegister() {

        return "register";
    }

    @RequestMapping("/tologin")
    public  String toLogin(){
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestParam String info,
                          @RequestParam String password,
                          HttpSession session,
                          HttpServletResponse response,
                          Model model) {
        Map map = userService.getLoginMap(info,password);
        Util.handleWebUser(session,response, map);
        model.addAttribute("loginMap", map);
        return "result";
    }


    @RequestMapping("/tocertification")
    public  String toCertification(HttpSession session){
        if(!Util.isSessionValid(session)) {
            return "rediret:register";
        }
        return "certification";
    }

    @RequestMapping(value="/certification", method = RequestMethod.POST)
    public String certification(@RequestParam String chinesename,
                                @RequestParam String idcard,
                                HttpSession session,
                                Model model) {
        if(!Util.isSessionValid(session)) {
            return "rediret:register";
        }
        String userName = (String)session.getAttribute("userName");
        Map map = userService.getCertificationMap(userName, chinesename, idcard);
        model.addAttribute("certificationMap",map);
        return  "result";
    }

}
